const maintabs = {
    state:{
        tabs:[
            {route:'/home/first',name:'首页',closable:false}
        ],
        activeRoute:'/home/first',
        menuList:[
            {
                name:"非菜单的路由，但是需要显示tab",
                children: [
                    {name: '个人中心',path:'/home/personal'}
                ]
            },
            {
                id:0,
                name:"首页",
                path:'/home/first',
                icon:"el-icon-house",
                children:[]
            },
            {
                id:1,
                name:"系统管理",
                path:"/system",
                icon:"el-icon-lock",
                children:[
                    {id:3,name:"用户管理",path:"/home/user"},
                    {id:4,name:"角色管理",path:"/home/role"},
                ]
            },
            {
                id:2,
                name:"订单管理",
                path:"/order",
                icon:"el-icon-eleme",
                children:[
                    {id:5,name:"订单详情",path:"/home/order"},
                ]
            }
        ],
        addTabName:''
    },
    mutations: {
        addTab(state,route){
            let isAlreadyIn = state.tabs.some(item => item.route === route)
            this.commit('findMenuNameByRoute',route);
            state.activeRoute = route
            if (!isAlreadyIn&&this.state.addTabName!==''&&this.state.addTabName!=='/'&&this.state.addTabName!=='/login'){
                console.log('添加Tab'+route)
                state.tabs.push({route:route,name: state.addTabName,closable:true})
            }
        },
        removeTab(state,route){
            if (route!='/home/first'){
                state.tabs = state.tabs.filter(item => item.route!== route)
                state.activeRoute = state.tabs[state.tabs.length-1].route
            }
        },
        findMenuNameByRoute(state,route){
            let findOne;
            for (let i of state.menuList){
                let tempArr = i.children.filter(item => item.path===route)
                if (tempArr.length>0){
                    findOne = tempArr[0]
                    break;
                }
            }
            state.addTabName = findOne?findOne.name:''
        }
    },
    actions: {
    },
    getters:{

    }
}

export default maintabs
